<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
$lotto_id = intval($_GET['id']);
$bonum = intval($_GET['bonum']);
$conf = new Config();
$startbet = $conf->get("startbet");
$stopbet = $conf->get("waitbet");
$prot_start = $conf->get("protect_start");
if (!isset($_SESSION['bsaystartmod']))
{
    $_SESSION['bsaystartmod'] = 0;
}
$saystartmod = $_SESSION['bsaystartmod'];
if (!isset($_SESSION['bsaywaitmod']))
{
    $_SESSION['bsaywaitmod'] = 0;
}
$saystopmod = $_SESSION['bsaywaitmod'];
if ($stopbet <= date("Hi") && !$saystopmod)
{
    $saystopmod = 1;
    $_SESSION['bsaywaitmod'] = $saystopmod;
    echo "-1";
    exit();
}
$stopmode = $prot_start == 0 ? 1 : $saystopmod;
if ($saystopmod == 0 && $prot_start != $saystartmod)
{
    $saystartmod = $prot_start;
    $_SESSION['bsaystartmod'] = $saystartmod;
    $stopmode = $saystartmod;
    echo "-1";
    exit();
}
$amounts = array();
$counts = array();
$totalamount = array();
$totalcount = array();
$betids = array(4, 5, 6, 31);
$total_win = array();
$ary = array();
foreach ($betids as $betid)
{
    $total_win[$betid] = 0;
    $ary[$betid] = array();
}
$i = 1;
while ($i < 50)
{
    $ary[4][$i] = array(
        "num" => $i,
        "rate" => 0,
        "amount" => 0,
        "count" => 0,
        "lost" => 0
    );
    $ary[31][$i] = array(
        "num" => $i,
        "rate" => 0,
        "amount" => 0,
        "count" => 0,
        "lost" => 0
    );
    ++$i;
}
$ary[5][1] = array("rate" => 0, "amount" => 0, "count" => 0, "lost" => 0);
$ary[5][2] = array("rate" => 0, "amount" => 0, "count" => 0, "lost" => 0);
$ary[6][1] = array("rate" => 0, "amount" => 0, "count" => 0, "lost" => 0);
$ary[6][2] = array("rate" => 0, "amount" => 0, "count" => 0, "lost" => 0);
$rs = getdb("select sum(amount) from bet2 where user_id=" . $curruser['id'] . " and lotto_id=" . $lotto_id . " and rame in (0,9)");
$usedamount = $rs->f[0] ? $rs->f[0] : 0;
$rs->close();
$users = GetUserList($curruser['id']);
if ($users != "")
{
    $rs = getdb("select SUM(amount_6h) from `k_user_bet` where user_id in ({$users}) and lotto_id={$lotto_id}");
    $usedamount += $rs->f[0] ? $rs->f[0] : 0;
    $rs->close();
}
$usedamount = $curruser['amount'] - $usedamount;
$sql = "select * from bet where lotto_id=" . $lotto_id . " and bettype in (4,5,6,31) and rame in (0,9)";
$theacl = $curruser['acl'];
$userid = $curruser['id'];
$userlist = GetUserList($userid);
if ($userlist == "")
{
    exit("Not Users!");
    exit();
}
$sql .= " and user_id in (" . $userlist . ") order by bettype";
if ($bonum < $curruser['alert40'])
{
    getdb("update k_user set alert40=" . $bonum . " where id=" . $curruser['id']);
}
$rs = getdb($sql);
while (!$rs->eof)
{
    $percent = 1;
    $backs = 100;
    if ($theacl == 5)
    {
        $backs = $rs->f['back4'];
        $percent = $rs->f['percent4'];
    }
    else if ($theacl == 4)
    {
        $backs = $rs->f['back3'];
        $percent = $rs->f['percent3'];
    }
    else if ($theacl == 3)
    {
        $backs = $rs->f['back2'];
        $percent = $rs->f['percent2'];
    }
    else
    {
        $backs = $rs->f['back1'];
        $percent = $rs->f['mpercent'];
    }
    if (0 < $percent)
    {
        $percent /= 100;
    }
    $tmp = $percent * $rs->f['amount'];
    $num = intval($rs->f['numbers']);
    $ary[$rs->f['bettype']][$num]['amount'] += $tmp;
    ++$ary[$rs->f['bettype']][$num]['count'];
    $ary[$rs->f['bettype']][$num]['lost'] += $tmp * $rs->f['rate'];
    $total_win[$rs->f['bettype']] += $tmp * ( $backs / 100 );
    $rs->movenext();
}
$rs->close();
if (2 < $theacl)
{
    $sql = "select * from bet2 where lotto_id=" . $lotto_id . " and uperid" . $theacl . "=" . $userid . " and bettype in (4,5,6,31) and rame in (0,9) order by bettype";
    $rs = getdb($sql);
    while (!$rs->eof)
    {
        $percent = 1;
        $backs = 100;
        if ($theacl == 5)
        {
            $backs = $rs->f['back5'];
            $percent = $rs->f['percent5'];
        }
        else if ($theacl == 4)
        {
            $backs = $rs->f['back4'];
            $percent = $rs->f['percent4'];
        }
        else if ($theacl == 3)
        {
            $backs = $rs->f['back3'];
            $percent = $rs->f['percent3'];
        }
        if (0 < $percent)
        {
            $percent /= 100;
        }
        if ($rs->f['prev_id'] == 0)
        {
            $percent = 0;
        }
        $tmp = $percent * $rs->f['amount'];
        $num = intval($rs->f['numbers']);
        $ary[$rs->f['bettype']][$num]['amount'] += $tmp;
        ++$ary[$rs->f['bettype']][$num]['count'];
        $ary[$rs->f['bettype']][$num]['lost'] += $tmp * $rs->f['rate'];
        $total_win[$rs->f['bettype']] += $tmp * ( $backs / 100 );
        $rs->movenext();
    }
}
$sql = "select * from bet2 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and bettype in (4,5,6,31) and rame in (0,9) order by bettype";
$rs = getdb($sql);
while (!$rs->eof)
{
    $backs = 100;
    if ($theacl == 5)
    {
        $backs = $rs->f['back5'];
    }
    else if ($theacl == 4)
    {
        $backs = $rs->f['back4'];
    }
    else if ($theacl == 3)
    {
        $backs = $rs->f['back3'];
    }
    else if ($theacl == 2)
    {
        $backs = $rs->f['back2'];
    }
    $tmp = $rs->f['amount'];
    $num = intval($rs->f['numbers']);
    $ary[$rs->f['bettype']][$num]['amount'] -= $tmp;
    $ary[$rs->f['bettype']][$num]['lost'] -= $tmp * $rs->f['rate'];
    $total_win[$rs->f['bettype']] -= $tmp * ( $backs / 100 );
    $rs->movenext();
}
$rs = getdb("select * from rate4a where lotto_id=" . $lotto_id);
if ($rs->eof)
{
    exit("Error ID!");
}
if ($theacl == 5)
{
    $i = 1;
    while ($i < 50)
    {
        $ary[4][$i]['rate'] = $rs->f["r" . d2($i)] - 0;
        ++$i;
    }
}
else
{
    $rsu = get_srates_data($userid, $lotto_id, 4);
    $i = 1;
    while ($i < 50)
    {
        $ary[4][$i]['rate'] = $rs->f["r" . d2($i)] - $rsu->f["r" . d2($i)] - 0;
        ++$i;
    }
}
$rs->close();
$j = 1;
while ($j < 50)
{
    $ary[4][$j]['amount'] += $ary[31][$j]['amount'];
    $ary[4][$j]['count'] += $ary[31][$j]['count'];
    $ary[4][$j]['lost'] += $ary[31][$j]['lost'];
    ++$j;
}
$total_win[4] += $total_win[31];
$i = 1;
while ($i < 49)
{
    $j = $i + 1;
    while ($j < 50)
    {
        if ($ary[4][$i]['lost'] < $ary[4][$j]['lost'])
        {
            $tmp = $ary[4][$i];
            $ary[4][$i] = $ary[4][$j];
            $ary[4][$j] = $tmp;
        }
        ++$j;
    }
    ++$i;
}
$rs = getdb("select * from rate5a where lotto_id=" . $lotto_id);
if ($rs->eof)
{
    exit("Error ID!");
}
$ary[5][1]['rate'] = $rs->f['r5_1'] - 0;
$ary[5][2]['rate'] = $rs->f['r5_2'] - 0;
$ary[6][1]['rate'] = $rs->f['r6_1'] - 0;
$ary[6][2]['rate'] = $rs->f['r6_2'] - 0;
$rs->close();
echo $usedamount . "@" . $total_win[4];
$i = 1;
while ($i < 50)
{
    echo "," . $ary[4][$i]['num'] . "@" . $ary[4][$i]['rate'] . "@" . $ary[4][$i]['count'] . "@" . $ary[4][$i]['amount'];
    echo "@" . $ary[4][$i]['lost'] . "@" . $stopmode;
    ++$i;
}
echo "\n";
echo $total_win[5];
$i = 1;
while ($i < 3)
{
    echo "," . $ary[5][$i]['rate'] . "@" . $ary[5][$i]['count'] . "@" . $ary[5][$i]['amount'];
    echo "@" . $ary[5][$i]['lost'] . "@" . $stopmode;
    ++$i;
}
echo "\n";
echo $total_win[6];
$i = 1;
while ($i < 3)
{
    echo "," . $ary[6][$i]['rate'] . "@" . $ary[6][$i]['count'] . "@" . $ary[6][$i]['amount'];
    echo "@" . $ary[6][$i]['lost'] . "@" . $stopmode;
    ++$i;
}
echo "\n";
include( "class_6h/class.menu.php" );
$fieldArr = array(
    "lotto_id" => $lotto_id,
    "split_id" => 1
);
$onlineMenu = new menu_6h(12, $fieldArr);
echo $onlineMenu->getMenuAmu();
echo "\n";
exit();